package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.dto.CldMaterialConsumeDto;
import com.cleaningcloud.cld.domain.vo.CldMaterialConsumeDetailsVo;
import com.cleaningcloud.cld.domain.vo.CldMaterialConsumeVo;
import com.cleaningcloud.cld.mapper.CldMaterialConsumeMapper;
import com.cleaningcloud.cld.service.ICldMaterialConsumeService;
import com.cleaningcloud.common.core.constant.SecurityConstants;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.RemoteCommodityService;
import com.cleaningcloud.erp.api.RemoteWareService;
import com.cleaningcloud.erp.api.domain.ErpCommodityStockAlter;
import com.cleaningcloud.erp.api.domain.ErpWareCommodityNumber;
import com.cleaningcloud.erp.api.utils.StockCommodityAlterUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 消耗记录Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-28
 */
@Service
public class CldMaterialConsumeServiceImpl implements ICldMaterialConsumeService {
    @Autowired
    private CldMaterialConsumeMapper cldMaterialConsumeMapper;

    @Autowired
    private RemoteCommodityService remoteCommodityService;

    @Autowired
    private RemoteWareService remoteWareService;

    /**
     * 查询消耗记录
     *
     * @param materialConsumeId 消耗记录主键
     * @return 消耗记录
     */
    @Override
    public CldMaterialConsumeVo selectCldMaterialConsumeByMaterialConsumeId(Long materialConsumeId) {
        CldMaterialConsumeVo cldMaterialConsumeVo = cldMaterialConsumeMapper.selectCldMaterialConsumeByMaterialConsumeId(materialConsumeId);
        for (CldMaterialConsumeDetailsVo materialConsumeDetailsVo : cldMaterialConsumeVo.getDetailsList()) {
            materialConsumeDetailsVo.setCommodityType(CommunityTypeConversionUtils.conversion(materialConsumeDetailsVo.getCommodityType()));
        }
        return cldMaterialConsumeVo;
    }

    /**
     * 查询消耗记录列表
     *
     * @param cldMaterialConsume 消耗记录
     * @return 消耗记录
     */
    @Override
    public List<CldMaterialConsumeVo> selectCldMaterialConsumeList(CldMaterialConsumeDto cldMaterialConsume) {
        cldMaterialConsume.getParams().put("isStaffGroup", cldMaterialConsume.getIsStaffGroup() == null ? 0 : cldMaterialConsume.getIsStaffGroup());
        List<CldMaterialConsumeVo> list = cldMaterialConsumeMapper.selectCldMaterialConsumeList(cldMaterialConsume);
        for (CldMaterialConsumeVo materialConsumeVo : list) {
            for (CldMaterialConsumeDetailsVo materialConsumeDetailsVo : materialConsumeVo.getDetailsList()) {
                materialConsumeDetailsVo.setCommodityType(CommunityTypeConversionUtils.conversion(materialConsumeDetailsVo.getCommodityType()));
            }
        }
        return list;
    }

    @Override
    public List<CldMaterialConsumeVo> selectCldMaterialConsumeProjectList(CldMaterialConsumeDto cldMaterialConsumeDto) {
        return cldMaterialConsumeMapper.selectCldMaterialConsumeProjectList(cldMaterialConsumeDto);
    }

    /**
     * 新增消耗记录
     *
     * @param cldMaterialConsume 消耗记录
     * @return 结果
     */
    @Override
    @Transactional
    @GlobalTransactional(rollbackFor = Exception.class)
    public int insertCldMaterialConsume(CldMaterialConsumeDto cldMaterialConsume) {
        cldMaterialConsume.setCreateTime(DateUtils.getNowDate());
        cldMaterialConsume.setConsumeId(SecurityUtils.getUserId());
        cldMaterialConsume.setConsumeTime(DateUtils.getNowDate());
        cldMaterialConsume.setConsumeOdd("BS" + UniqueIdUtils.generateUniqueId());
        cldMaterialConsume.setCreateName(SecurityUtils.getUsername());
        cldMaterialConsume.setFlag(0L);
        cldMaterialConsumeMapper.insertCldMaterialConsume(cldMaterialConsume);
        // 商品库存信息
        ErpWareCommodityNumber query = new ErpWareCommodityNumber();
        if (cldMaterialConsume.getDetailsList() != null && !cldMaterialConsume.getDetailsList().isEmpty()) {
            cldMaterialConsume.getDetailsList().forEach(item -> {
                item.setMaterialConsumeId(cldMaterialConsume.getMaterialConsumeId());
                // 修改对应库存
                query.setWareId(cldMaterialConsume.getConsumeWareId());
                query.setCommodityId(item.getCommodityId());
                ErpWareCommodityNumber out = remoteWareService.selectByCommodityIdAndWareId(query, SecurityConstants.INNER);
                // 根据消耗数量进行库存相减
                if (out.getCommodityNumber() < item.getCommodityNumber()) {
                    throw new RuntimeException("当前仓库库存数量小于调拨数量");
                }
                out.setCommodityNumber(out.getCommodityNumber() - item.getCommodityNumber());
                remoteWareService.updateErpWareCommodityNumber(out, SecurityConstants.INNER);
                // 新增库存商品变更记录信息
                ErpCommodityStockAlter erpCommodityStockAlter = StockCommodityAlterUtils.AssemblyInfo(item.getCommodityId(), BigDecimal.valueOf(item.getCommodityNumber()).negate(), "4", cldMaterialConsume.getConsumeWareId());
                remoteCommodityService.addWare(erpCommodityStockAlter, SecurityConstants.INNER);
            });
            cldMaterialConsumeMapper.batchInsertMaterialConsumeDetails(cldMaterialConsume.getDetailsList());
        }
        return 1;
    }

    /**
     * 修改消耗记录
     *
     * @param cldMaterialConsume 消耗记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCldMaterialConsume(CldMaterialConsumeDto cldMaterialConsume) {
        cldMaterialConsumeMapper.updateCldMaterialConsume(cldMaterialConsume);
        // 先删除
        cldMaterialConsumeMapper.deleteMaterialConsumeDetailsByWareAllocateId(cldMaterialConsume.getMaterialConsumeId());
        if (cldMaterialConsume.getDetailsList() != null && !cldMaterialConsume.getDetailsList().isEmpty()) {
            cldMaterialConsume.getDetailsList().forEach(item -> {
                item.setMaterialConsumeId(cldMaterialConsume.getMaterialConsumeId());
            });
            cldMaterialConsumeMapper.batchInsertMaterialConsumeDetails(cldMaterialConsume.getDetailsList());
        }
        return 1;
    }

    /**
     * 批量删除消耗记录
     *
     * @param materialConsumeIds 需要删除的消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteCldMaterialConsumeByMaterialConsumeIds(Long[] materialConsumeIds) {
        return cldMaterialConsumeMapper.deleteCldMaterialConsumeByMaterialConsumeIds(materialConsumeIds);
    }

    /**
     * 删除消耗记录信息
     *
     * @param materialConsumeId 消耗记录主键
     * @return 结果
     */
    @Override
    public int deleteCldMaterialConsumeByMaterialConsumeId(Long materialConsumeId) {
        return cldMaterialConsumeMapper.deleteCldMaterialConsumeByMaterialConsumeId(materialConsumeId);
    }
}
